package com.csi.bumblebee.controller;


import com.csi.bumblebee.domain.user.MyJob;
import com.csi.bumblebee.domain.user.MyCollection;
import com.csi.bumblebee.domain.user.Student;
import com.csi.bumblebee.domain.user.Submit;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.domain.vo.MyJobReturnVo;
import com.csi.bumblebee.service.JobService;
import com.csi.bumblebee.service.SubmitService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.csi.bumblebee.domain.util.StateCode.*;

/**
 * @author 韦典良
 * @author HUOJT
 */
@RestController
@RequestMapping("/job")
@CrossOrigin
public class JobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private SubmitService submitService;

    /**
     * 查询岗位信息详情页
     */
    @GetMapping("/jobmain/{jobId}")
    public MyResult<MyJob> findByIdJob(@PathVariable("jobId") String jobId) {

        MyJob job = this.jobService.findByIdJob(jobId);

        if (job == null) {
            return new MyResult<>(FIND_FAILURE, "未查询到相关岗位信息！");
        }

        return new MyResult<>(job, StateCode.FIND_SUCCESS, "查询岗位信息成功！");

    }

    /**
     * 收藏岗位(传入学生id和岗位id)
     */
    @PostMapping("/likeJob")
    public MyResult<Object> likeJob(@RequestBody MyCollection myCollection) {

        int num = this.jobService.likeJob(myCollection);

        //如果收藏成功
        if (num > 0) {
            return new MyResult<>(num, StateCode.INSERT_SUCCESS, "收藏岗位成功！");
        }

        return new MyResult<>(num, StateCode.INSERT_FAILURE, "收藏岗位失败！");

    }


    /**
     * 取消收藏岗位(传入学生id和岗位id)
     */
    @PutMapping("/outJob")
    public MyResult<Integer> deleteLikeJob(@RequestBody MyCollection myCollection) {

        int num = this.jobService.deleteLikeJob(myCollection);

        //如果取消收藏成功
        if (num > 0) {
            return new MyResult<>(num, StateCode.DELETE_SUCCESS, "取消收藏岗位成功！");
        }

        return new MyResult<>(num, StateCode.DELETE_FAILURE, "取消收藏岗位失败！");

    }


    @GetMapping("/test")
    public Object test(@RequestParam Integer id) {

        List<Student> list = new ArrayList<>();
        Student student = new Student();
        student.setId("123");
        student.setUsername("数码产品");

        list.add(student);

        return list;

    }

    /**
     * 首页偏好岗位列表
     *
     * @param studentId 学生id
     * @return 列表
     */
    @GetMapping("/favourite/{studentId}")
    public MyResult<List<MyJobReturnVo>> favourite(@PathVariable(required = true) String studentId) {
        MyResult<List<MyJobReturnVo>> myResult = new MyResult<>(FIND_FAILURE, "查询失败");
        List<MyJobReturnVo> myJobReturnVos = jobService.favouriteList(studentId);
        if (myJobReturnVos != null) {
            myResult.setData(myJobReturnVos);
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询成功");
        }
        return myResult;
    }

    /**
     * 筛选岗位列表
     *
     * @param myJobReturnVo vo
     * @return 列表
     */
    @PostMapping("selection")
    public MyResult<PageInfo<MyJobReturnVo>> selectList(@RequestBody MyJobReturnVo myJobReturnVo) {
        MyResult<PageInfo<MyJobReturnVo>> myResult = new MyResult<>(FIND_FAILURE, "查询失败");
        PageInfo<MyJobReturnVo> myJobReturnVos = jobService.selectList(myJobReturnVo);
        if (myJobReturnVos != null) {
            myResult.setData(myJobReturnVos);
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询成功");
        }
        return myResult;
    }

    /**
     * 得到岗位类型对应的岗位列表
     *
     * @param jobTypeId 一级岗位类型id
     * @return 列表
     */
    @GetMapping("jobTypeId/{jobTypeId}")
    public MyResult<List<MyJobReturnVo>> listByType(@PathVariable String jobTypeId) {
        List<MyJobReturnVo> myJobReturnVos = jobService.listByType(jobTypeId);
        if (myJobReturnVos != null) {
            return new MyResult<>(myJobReturnVos, FIND_SUCCESS, "查询成功");
        }
        return new MyResult<>(FIND_FAILURE, "查询失败");
    }

    /**
     * 得到时间倒序对应的岗位列表
     *
     * @return 列表
     */
    @GetMapping("time/desc")
    public MyResult<List<MyJobReturnVo>> listByTime() {
        List<MyJobReturnVo> myJobReturnVos = jobService.listByTime();
        if (myJobReturnVos != null) {
            return new MyResult<>(myJobReturnVos, FIND_SUCCESS, "查询成功");
        }
        return new MyResult<>(FIND_FAILURE, "查询失败");
    }

    /**
     * 收藏的岗位列表
     *
     * @param studentId 学生id
     * @return 列表
     */
    @GetMapping("collection/student/{studentId}")
    public MyResult<PageInfo<MyJobReturnVo>> collectionListByStudent(@PathVariable String studentId, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo<MyJobReturnVo> myJobReturnVos = jobService.collectionListByStudent(studentId, pageNum);
        if (myJobReturnVos != null) {
            return new MyResult<>(myJobReturnVos, FIND_SUCCESS, "查询收藏岗位数据成功");
        }
        return new MyResult<>(FIND_FAILURE, "查询收藏岗位数据失败");
    }

    /**
     * 投递的岗位列表
     *
     * @param studentId 学生id
     * @return 列表
     */
    @GetMapping("submitted/student/{studentId}")
    public MyResult<PageInfo<MyJobReturnVo>> submittedListByStudent(@PathVariable String studentId, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo<MyJobReturnVo> myJobReturnVos = jobService.submittedListByStudent(studentId, pageNum);
        if (myJobReturnVos != null) {
            return new MyResult<>(myJobReturnVos, FIND_SUCCESS, "查询投递岗位数据成功");
        }
        return new MyResult<>(FIND_FAILURE, "查询投递岗位数据失败");
    }

    /**
     * 查询是否已经被收藏
     *
     * @param studentId 学生id
     * @param jobId     岗位id
     * @return 是或否
     */
    @GetMapping("OneLiked")
    public MyResult<Boolean> findLikeJob(@RequestParam String studentId, @RequestParam String jobId) {
        MyCollection myCollection = new MyCollection();
        myCollection.setStudentId(studentId);
        myCollection.setJobId(jobId);
        List<MyCollection> myCollections = jobService.findLikeJob(myCollection);
        if (myCollections != null && myCollections.size() > 0) {
            return new MyResult<>(true, FIND_SUCCESS, "查询成功");
        }
        return new MyResult<>(false, FIND_SUCCESS, "查询成功");
    }

    /**
     * 简历投递
     *
     * @param studentId 学生id
     * @param jobId     岗位id
     * @return 是或否
     */
    @GetMapping("submit")
    public MyResult<Boolean> insertSubmit(@RequestParam String studentId, @RequestParam String jobId) {
        Submit submit = new Submit();
        submit.setJobId(jobId);
        submit.setStudentId(studentId);
        if (submitService.insert(submit) > 0) {
            return new MyResult<>(true, INSERT_SUCCESS, "投递成功");
        }
        return new MyResult<>(false, INSERT_FAILURE, "投递失败，您近期已经投递过了，请过一段时间重试。");
    }

    /**
     * 查询一个二级岗位分类下的所有已发出的岗位列表
     */
    @GetMapping("/jobstwo/{jobTypeId}")
    public MyResult<List<MyJob>> listJobTwos(@PathVariable("jobTypeId") String jobTypeId){
        System.out.println("jobTypeId-------------:"+jobTypeId);
        List<MyJob> myJobList = this.jobService.listJobTwos(jobTypeId);
        if (myJobList!=null && myJobList.size()>0){
            return new MyResult<>(myJobList, FIND_SUCCESS, "查询二级岗位分类下的所有已发出的岗位列表成功");
        }
        return new MyResult<>(myJobList, FIND_SUCCESS, "查询二级岗位分类下的所有已发出的岗位列表失败");
    }

}
